/* $RCSFile: ModulePropertypage.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/Modules/ModulePropertypage.aspx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:21  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:57  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:26  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.Core;
using AddonNice.Settings;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.UI;
using AddonNice.UI.WebControls;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.Modules
{
 	#region Local TraceSwitch 
	public class ModulePropertyTrace : ADNTraceSwitch
	{
		static public ADNTraceSwitch	Sw	=	new ADNTraceSwitch("ModulePropertyTrace","Special Trace Switch for ModulePropertyPage");
	}
	#endregion Local TraceSwitch 

    /// <summary>
	/// Module that allows to edit the settings for a module
	/// </summary>
	public partial class ModulePropertyPage : PropertyPage
	{

        ModulesDB mDB   =   new ModulesDB();


		private void Page_Load(object sender, EventArgs e)
        {
            if ( PageTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModulePropertyPage Page_Load BEG ModuleID: {0}", ModuleID),PageTraceSwitch.Sw.Info);
            //We reset cache before display page to ensure dropdown shows actual data
			AddonNice.Settings.Cache.CurrentCache.Remove(AddonNice.Settings.Cache.Key.ModuleSettings(ModuleID));
            if ( !IsPostBack )
                EditTable.DataBind();
            if ( PageTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModulePropertyPage Page_Load END ModuleID: {0}", ModuleID),PageTraceSwitch.Sw.Info);
        }
  
		/// <summary>
		/// We don't want to be limited by demo mode for this method
		/// </summary>
        protected override void OnAdminProperties(ADNPageEventArgs e) 
		{
            e.OverrideDemo  =   true;
            base.OnAdminProperties(e);
		}

        void ModulePropertyPage_AdminProperties(object sender, AddonNice.UI.ADNPageEventArgs e)
		{
			string url	=	HttpUrlBuilder.BuildUrl(Config.ModulesPath + "/" + Config.ModuleSettingsPage, PageID, ModuleID);
            if ( ModulePropertyTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModulePropertyPage ModulePropertyPage_AdminProperties url: {0}",url),ModulePropertyTrace.Sw.Info);
			// Redirect to module settings page
            Redirect(url,true);
		}

        /// <summary>
        /// Allow update in demo mode
        /// </summary>
        protected override void OnUpdate(ADNPageEventArgs e)
        {
            e.OverrideDemo  =   true;
            base.OnUpdate(e);
		}

        /// <summary>
		/// Persists the changes to database
		/// </summary>
        void ModulePropertyPage_Update(object sender, AddonNice.UI.ADNPageEventArgs e)
        {
            // Only Update if Input Data is Valid
            if ( ModulePropertyTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModulePropertyPage ModulePropertyPage_Update BEG ModuleID: {0}, IsValid: {1}", ModuleID, IsValid),ModulePropertyTrace.Sw.Info);
            // Update settings in the database
            EditTable.UpdateControls();
            if ( ModulePropertyTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModulePropertyPage ModulePropertyPage_Update END ModuleID: {0}", ModuleID),ModulePropertyTrace.Sw.Info);
        }

        protected override void OnCancel(EventArgs e) 
        {
            string redirectUrl  =   ReturnUrl;
            if ( ModulePropertyTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModulePropertyPage OnCancel redirectUrl: '{0}'",redirectUrl),ModulePropertyTrace.Sw.Info);
            if ( redirectUrl != string.Empty )
                Redirect(redirectUrl,true);
            Redirect(HttpUrlBuilder.BuildUrl(PageID),true);
		}

        private void EditTable_UpdateControl(object sender, SettingsTableItemEventArgs e)
        {
            string val          =   e.CurrentItem.Value;
            if ( SettingsTraceSwitch.Sw.TraceVerbose || ModulePropertyTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModulePropertyPage EditTable_UpdateControl BEG ModuleID: {0}, EditControl.ID: {1}, CurrentItem.Value: [{2}], CurrentItem.ParentDBId: [3]", 
                    new object[]{ModuleID,e.CurrentItem.EditControl.ID, val,e.CurrentItem.ParentDBId}),(SettingsTraceSwitch.Sw.TraceVerbose) ? SettingsTraceSwitch.Sw.Info:ModulePropertyTrace.Sw.Info);
            if (e.CurrentItem.Key != e.CurrentItem.EditControl.ID)
            {
                string msg      =   string.Format("[E]ModulePropertyPage EditTable_UpdateControl ModuleID: {0}, EditControl.ID: [{1}] != CurrentItem.Key: [{2}]", 
                                        ModuleID,e.CurrentItem.EditControl.ID, e.CurrentItem.Key);
            if ( SettingsTraceSwitch.Sw.TraceVerbose || ModulePropertyTrace.Sw.TraceVerbose )
                    Trace2.WriteLine( msg,(SettingsTraceSwitch.Sw.TraceVerbose) ? SettingsTraceSwitch.Sw.Info:ModulePropertyTrace.Sw.Info);
                throw new Exception(msg);
            }
            e.CurrentItem.UpdateModuleSetting(mDB);
            if ( SettingsTraceSwitch.Sw.TraceVerbose || ModulePropertyTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModulePropertyPage EditTable_UpdateControl END EditControl.ID: {0}", e.CurrentItem.EditControl.ID),(SettingsTraceSwitch.Sw.TraceVerbose) ? SettingsTraceSwitch.Sw.Info:ModulePropertyTrace.Sw.Info);
        }

        #region Std Overrides

        override protected void OnPreInit(EventArgs e)
        {
            if ( PageTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModulePropertyPage OnPreInit BEG.",PageTraceSwitch.Sw.Info);
            ImageButton iButton					=	new ImageButton();
			iButton.SkinID						=	"ModuleBaseParameters_"+Helpers.LocalizeHelper.GetCurrentNeutralCultureName();
			adminPropertiesButton				=	iButton;

			iButton								=	new ImageButton();
			iButton.SkinID						=	"ModuleBaseParameters_"+Helpers.LocalizeHelper.GetCurrentNeutralCultureName();
            adminPropertiesButton2				=	iButton;
            base.OnPreInit(e);
            if ( PageTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModulePropertyPage OnPreInit END.",PageTraceSwitch.Sw.Info);
        }

		override protected void OnInit(EventArgs e)
		{
            if ( PageTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModulePropertyPage OnInit BEG.",PageTraceSwitch.Sw.Info);
			InitializeComponent();
            base.OnInit(e);
            if ( PageTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModulePropertyPage OnInit END.",PageTraceSwitch.Sw.Info);
		}

		private void InitializeComponent()
		{
            Load                    +=  new System.EventHandler(this.Page_Load);
            Update                  +=  new PageEventHandler(ModulePropertyPage_Update);
            AdminProperties         +=  new PageEventHandler(ModulePropertyPage_AdminProperties);
            EditTable.UpdateControl +=  new UpdateControlEventHandler(EditTable_UpdateControl);

			PlaceHolderButtons.Controls.Add(adminPropertiesButton);
            PlaceHolderButtons2.Controls.Add(adminPropertiesButton2);
            PlaceHolderButtons.Controls.Add(new LiteralControl("&#160;"));
            PlaceHolderButtons2.Controls.Add(new LiteralControl("&#160;"));
		}


        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons
		{
			get
			{ return PlaceHolderButtons; }
			set { }
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons2
		{
			get
			{ return PlaceHolderButtons2; }
			set { }
		}
		#endregion
	}
}